package com.zxhhyj.ffmpeg.player

import android.media.AudioAttributes
import android.media.AudioFormat
import android.media.AudioManager
import android.media.AudioTrack

public class AudioPlayer(sampleRateInHz: Int, nbChannels: Int) {

    private fun createAudioTrack(sampleRateInHz: Int, nbChannels: Int): AudioTrack {
        val audioFormat = AudioFormat.ENCODING_PCM_16BIT
        // 根据声道个数，确定声道布局
        val channelConfig = when (nbChannels) {
            1 -> {
                AudioFormat.CHANNEL_OUT_MONO
            }

            2 -> {
                AudioFormat.CHANNEL_OUT_STEREO
            }

            else -> {
                AudioFormat.CHANNEL_OUT_STEREO
            }
        }

        val audioAttributes = AudioAttributes.Builder()
            .setLegacyStreamType(AudioManager.STREAM_MUSIC)
            .build()
        val audioFormat1 = AudioFormat.Builder()
            .setSampleRate(sampleRateInHz)
            .setEncoding(audioFormat)
            .setChannelMask(channelConfig)
            .build()

        val bufferSizeInBytes =
            AudioTrack.getMinBufferSize(sampleRateInHz, channelConfig, audioFormat)


        val audioTrack = AudioTrack(
            audioAttributes, audioFormat1, bufferSizeInBytes,
            AudioTrack.MODE_STREAM, AudioManager.AUDIO_SESSION_ID_GENERATE
        )
        return audioTrack
    }

    private val audioTrack = createAudioTrack(sampleRateInHz, nbChannels)

    public fun loadAudio(filePath: String) {

    }

    //private external fun nativePlay(fd: Int)

    public fun play() {

    }

    public fun pause() {

    }

    public fun release() {

    }
}